<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2016/12/21
 * Time: 10:45
 */

//ArrayAccess（数组式访问）接口 ¶
//class ArrayAccess {
//	/* 方法 */
//	abstract public boolean offsetExists ( mixed $offset )
//	abstract public mixed offsetGet ( mixed $offset )
//	abstract public void offsetSet ( mixed $offset , mixed $value )
//	abstract public void offsetUnset ( mixed $offset )
//}
class TestArrayAccess implements ArrayAccess {


	private $data = [];

	function __construct( array $data ) {
		$this->data = $data;
	}

	/**
	 * Whether a offset exists
	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
	 *
	 * @param mixed $offset <p>
	 * An offset to check for.
	 * </p>
	 *
	 * @return boolean true on success or false on failure.
	 * </p>
	 * <p>
	 * The return value will be casted to boolean if non-boolean was returned.
	 * @since 5.0.0
	 */
	public function offsetExists( $offset ) {
		return isset( $this->data[ $offset ] );
	}

	/**
	 * Offset to retrieve
	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
	 *
	 * @param mixed $offset <p>
	 * The offset to retrieve.
	 * </p>
	 *
	 * @return mixed Can return all value types.
	 * @since 5.0.0
	 */
	public function offsetGet( $offset ) {
		if ( $this->offsetExists( $offset ) ) {
			return $this->data[ $offset ];
		}
	}

	/**
	 * Offset to set
	 * @link http://php.net/manual/en/arrayaccess.offsetset.php
	 *
	 * @param mixed $offset <p>
	 * The offset to assign the value to.
	 * </p>
	 * @param mixed $value <p>
	 * The value to set.
	 * </p>
	 *
	 * @return void
	 * @since 5.0.0
	 */
	public function offsetSet( $offset, $value ) {
		if ( $this->offsetExists( $offset ) ) {
			$this->data[ $offset ] = $value;
		}
	}

	/**
	 * Offset to unset
	 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
	 *
	 * @param mixed $offset <p>
	 * The offset to unset.
	 * </p>
	 *
	 * @return void
	 * @since 5.0.0
	 */
	public function offsetUnset( $offset ) {
		if ( $this->offsetExists( $offset ) ) {
			unset( $this->data[ $offset ] );
		}
	}
}

$testArrayAccess = new TestArrayAccess( [ 1, 2, 3, 4, 5, 6 ] );

if($testArrayAccess->offsetExists(10)){
	echo "exists".PHP_EOL;
}else{
	echo "no exists".PHP_EOL;
}

$offsetGet = $testArrayAccess->offsetGet( 3 );
echo $offsetGet.PHP_EOL;

$testArrayAccess->offsetSet(3,333);
$offsetGet = $testArrayAccess->offsetGet( 3 );
echo $offsetGet.PHP_EOL;

